package bidsanity



import grails.test.mixin.*
import org.junit.*
import grailserrors.ErrorCodes

/**
 * Unit tests for the Bid domain class
 */
@TestFor(Bid)
class BidTests {
    def s = new Customer(emailAddress: 'seller@example.org', password: "testing")
    def w = new Customer(emailAddress: 'winner@example.org', password: "testing")
    def l = new Listing(
            name: 'TestListing',
            endDate: new Date() + 10,
            startingBidPrice: 1.0D,
            description: 'a test listing',
            seller: s,
            winner: w)

    void testAdd() {
        def b = new Bid(
                bidder: s,
                bidTime: new Date() - 10,
                amount: 1.0D,
                listing: l)
        b.save(failOnError: true)
        Assert.assertFalse b.hasErrors()

        Assert.assertNotNull b.bidder
        Assert.assertNotNull b.bidTime
        Assert.assertNotNull b.amount
        Assert.assertEquals Bid.list().size(), 1
    }

    void testNotNullFields() {
        def b = new Bid(bidder: null, bidTime: null, amount: null, listing: null)
        b.save(failOnError:false)
        Assert.assertTrue ErrorCodes.CheckErrorCode(b, 'amount', ErrorCodes.NULLABLE)
        Assert.assertTrue ErrorCodes.CheckErrorCode(b, 'bidTime', ErrorCodes.NULLABLE)
        Assert.assertTrue ErrorCodes.CheckErrorCode(b, 'bidder', ErrorCodes.NULLABLE)
        Assert.assertTrue ErrorCodes.CheckErrorCode(b, 'listing', ErrorCodes.NULLABLE)
    }

    void testDelete() {
        def b = new Bid(
                bidder: s,
                bidTime: new Date() - 10,
                amount: 1.0D,
                listing: l)
        b.save(failOnError: false)
        Assert.assertFalse b.hasErrors()
        Assert.assertEquals Bid.list().size(), 1
        
        b.delete()
        Assert.assertFalse b.hasErrors()
        Assert.assertEquals Bid.list().size(), 0
    }    
    
    void testAddNoListing() {
        def b = new Bid(
                bidder: s,
                bidTime: new Date() - 10,
                amount: 1.0D)
        b.save(failOnError: false)
        Assert.assertTrue b.hasErrors()
        Assert.assertEquals Bid.list().size(), 0
    }
    void testAddNoBidder() {
        def b = new Bid(
                bidTime: new Date() - 10,
                amount: 1.0D,
                listing: l)
        b.save(failOnError: false)
        Assert.assertTrue b.hasErrors()
        Assert.assertEquals Bid.list().size(), 0
    }

    void testAddNoBidTime() {
        def b = new Bid(
                bidder: s,
                amount: 1.0D,
                listing: l,
                bidTime: null)
        b.save(failOnError: false)
        Assert.assertTrue b.hasErrors()
        Assert.assertEquals Bid.list().size(), 0
    }

    void testAddNoAmount() {
        def b = new Bid(
                bidder: s,
                bidTime: new Date() - 10,
                listing: l)
        b.save(failOnError: false)
        Assert.assertTrue b.hasErrors()
        Assert.assertEquals Bid.list().size(), 0
    }
}
